<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/2/8
 * Time: 下午3:47
 */

namespace app\agent\controller;


use app\common\controller\AdminBase;
use app\common\model\Agent;
use app\common\model\WxOpen;
use app\common\model\XcxSet;
use think\Controller;
use think\Request;
use Wechat\Wxobjopen;

class Xcxsysset extends AdminBase
{
    public function verset()
    {
        $XcxSet = new XcxSet();

        $options = $this->options();
        $wxObj = new Wxobjopen($options);
        $ss = $wxObj->xcx_sys_templ();
        $data = [];
        if ($ss['errcode'] == 0) {
            $data = $ss['draft_list'];
        }
        $data2 = [];
        $ss2 = $wxObj->xcx_tpl_list();
        if ($ss2['errcode'] == 0) {
            $data2 = $ss2['template_list'];
        }
        $xcxset = $XcxSet->find();
        $this->assign('xcxset', $xcxset);
        $this->assign('data', $data);
        $this->assign('data2', $data2);
        return $this->fetch();
    }

    //设置服务器域名
    public function set_xcx_domain()
    {
        $options = $this->options();
        $wxObj = new Wxobjopen($options);
        $Agent= new Agent();
        $agentid = $this->get_agent_id();
        $info = $Agent->where(['id' =>$agentid])->find();
        $param['action'] = "add";
        $param['requestdomain'] = $info['requestdomain'];
        $param['wsrequestdomain'] = $info['wsrequestdomain'];
        $param['uploaddomain'] = $info['uploaddomain'];
        $param['downloaddomain'] = $info['downloaddomain'];
        if (request()->isAjax()) {
            $arr = request()->post();
            $data['requestdomain'] = $arr['requestdomain'];
            $data['wsrequestdomain'] = $arr['wsrequestdomain'];
            $data['uploaddomain'] = $arr['uploaddomain'];
            $data['downloaddomain'] = $arr['downloaddomain'];
            $res = $Agent->allowField(true)->save($data,['id'=>$agentid]);
            if ($res) {
                $data['code'] = 0;
                $data['message'] = "操作成功";
            } else {
                $data['code'] = 9001;
                $data['message'] = '操作失败';
            }
            return json($data);
        } else {

           
            $this->assign('info', $info);
            return $this->fetch();
        }

    }

    //开放平台设置
    public function set_xcx_openplat(){
        $agentid = $this->get_agent_id();
        $Agent= new Agent();
        if (Request()->isAjax()){
            $arr = request()->post();
            $data['component_appid'] = $arr['component_appid'];
            $data['component_appsecret'] = $arr['component_appsecret'];
            $data['component_token'] = $arr['component_token'];
            $data['component_encodingaeskey'] = $arr['component_encodingaeskey'];
            $res = $Agent->allowField(true)->save($data,['id'=>$agentid]);
            if ($res) {
                $data['code'] = 0;
                $data['message'] = "操作成功";
            } else {
                $data['code'] = 9001;
                $data['message'] = '操作失败';
            }
            return json($data);
        }else {
            $info = $Agent->where(['id' => $agentid])->find();
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

    public function addtpl()
    {
        if (request()->isAjax()) {
            $options = $this->options();
            $wxObj = new Wxobjopen($options);
            $param['draft_id'] = request()->post('draft_id');
            $ss = $wxObj->xcx_addtotpl($param);
            if ($ss['errcode'] == 0) {
                $data['code'] = 0;
                $data['message'] = "设置成功";
            } else {
                $data['code'] = $ss['errcode'];
                $data['message'] = $ss['errmsg'];
            }
            return json($data);
        }

    }

    public function set_tpl()
    {
        if (request()->isAjax()) {
            $data['template_id'] = request()->post('template_id');
            $data['extpages'] = request()->post('extpages');
            $data['user_version'] = request()->post('user_version');
            $data['weburl'] = request()->post('weburl');
            $data['user_desc'] = request()->post('user_desc');
            $WxXcxSet = new XcxSet();
            $up = $WxXcxSet->save($data, ['id' => 1]);
            if ($up) {
                $datas['code'] = 0;
                $datas['message'] = "设置成功";
            } else {
                $datas['code'] = 1;
                $datas['message'] = "设置失败";
            }
            return json($datas);
        }
    }

    public function del_tpl()
    {
        if (request()->isAjax()) {
            $options = $this->options();
            $wxObj = new Wxobjopen($options);
            $param['template_id'] = request()->post('template_id');
            $ss = $wxObj->xcx_tpl_del($param);
            if ($ss['errcode'] == 0) {
                $data['code'] = 0;
                $data['message'] = "删除成功";
            } else {
                $data['code'] = $ss['errcode'];
                $data['message'] = $ss['errmsg'];
            }
            return json($data);
        }
    }

    public function options()
    {
        $Agent = new Agent();
        $agent = $this->get_agent_id();
        $config = $Agent->where(['id' =>$agent])->find();

        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
        );
        return $options;
    }

}